import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col, Card, DatePicker} from 'antd';
import WrappedSelect from '../common/WrappedSelect';
import {partChannel} from '../Enum';
import {FORM_OPTIONS} from '../constants';
import {injectIntl, language} from './intl';
import moment from 'moment';
const {MonthPicker} = DatePicker;
const FormItem = Form.Item;

const partChannelList = partChannel.toList(language);

class QueryPanel extends React.PureComponent {
    state = {
        data: {},
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
        if(name === 'channel' && this.props.conditions.dealerId.length > 0)
            this.props.querySearchCountry();
    }
    disabledDate = current => current && current > moment().endOf('day')
    onChangeMonthPicker = (value, name) => {
        this.props.onConditionsChange('month', name);
    }
    render() {
        const {formatMessage} = this.props.intl;
        const condition = this.props.conditions || {};
        return (
            <div>
                <Form className="form-standard">
                    <Card>
                        <Row>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.channel',
                                        defaultMessage: '渠道'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        name="channel"
                                        value={condition.channel}
                                        options={partChannelList}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.month',
                                        defaultMessage: '年月'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <MonthPicker
                                        allowClear={false}
                                        placeholder="Select Month"
                                        disabledDate={this.disabledDate}
                                        value={condition.month ? moment(condition.month) : undefined}
                                        name={condition.month}
                                        onChange={this.onChangeMonthPicker} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col>
                                <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>{formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}</Button>
                                <Button key="clear" onClick={this.props.onResetClick}>{formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}</Button>
                            </Col>
                        </Row>
                    </Card>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    querySearchCountry: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {saveQueryCondition, querySearchMonth, resetQueryPanel, saveQueryDealer, querySearchCountry} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'listRate', 'isFetching']),
    queryDealer: getQueryDealer(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearchMonth()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onSelect: data => dispatch(saveQueryDealer(data)),
    querySearchCountry: value => dispatch(querySearchCountry(value)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
